﻿@section body {
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                @if (ViewBag.CoreMenuNew)
                {
                    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add" id="btnAdd" onclick="OnAdd();">新增</button>
                }
                <table class="layui-hidden" id="treeTable" lay-filter="treeTable"></table>
            </div>
        </div>
    </div>
    <script type="text/html" id="checkboxTpl">
        <i class="layui-icon {{d.ImageUrl}}"></i>
    </script>
    <script type="text/html" id="barDemo">
        @if (ViewBag.CoreMenuEdit)
        {
            <a class="pear-btn pear-btn-primary pear-btn-xs" lay-event="edit">编辑</a>
        }
        @if (ViewBag.CoreMenuDelete)
        {
            <a class="pear-btn pear-btn-danger pear-btn-xs" lay-event="delete">删除</a>
        }
    </script>
}



@section script {
    <script>
        var editObj = null, ptable = null, treeGrid = null, tableId = 'treeTable', layer = null, $;
        var common;
        layui.use(['table', 'jquery', 'view', 'treeGrid','common'], function () {
            $ = layui.$;
            var view = layui.view;
            treeGrid = layui.treeGrid;//很重要
            layer = layui.layer;
            common = layui.common;

            ptable = treeGrid.render({
                id: tableId
                , elem: '#' + tableId
                //, url: '../layuiadmin/json/treeGrid/treeData.json'
                , url: 'PostMenuListJson'
                , cellMinWidth: 100//单元格最小宽度
                , idField: 'ID'//必須字段
                , treeId: 'ID'//树形id字段名称
                , treeUpId: 'ParentID'//树形父id字段名称
                , treeShowName: 'Name'//以树形式显示的字段
                , heightRemove: [".dHead", 20]//不计算的高度,表格设定的是固定高度，此项不生效
                , height: 'full-80'
                , isFilter: false
                , iconOpen: true//是否显示图标【默认显示】
                , isOpenDefault: true//节点默认是展开还是折叠【默认展开】
                , loading: true
                , method: 'post'
                , isPage: false
                , cols: [[
                    { type: 'numbers' }
                    , { field: 'Name', width: 200, title: '菜单名称' }
                    , {
                        templet: function (d) {
                            if (d.Powers == null) {
                                return '无';
                            } else {
                                return d.Powers.Name;
                            }
                        },
                        field: '', width: 150, title: '浏览权限'

                    }
                    , { field: 'SortIndex', width: 80, title: '排序' }
                    , { field: 'NavigateUrl', width: 300, title: '链接' }
                    , { field: 'ImageUrl', title: '图标', width: 100, templet: '#checkboxTpl' }
                    , { field: 'Remark', title: '备注' }
                    , { title: '操作', toolbar: '#barDemo', width: 150 }
                ]]
                , where: { __RequestVerificationToken: document.getElementsByName("__RequestVerificationToken")[0].value }
                , done: function (res, curr, count) {

                }

            });
            treeGrid.on('tool(' + tableId + ')', function (obj) {
                if (obj.event === 'edit') {//编辑
                    top.layer.open({
                        type: 2,
                        resize: false,
                        content: '/Admin/MenuEdit?id=' + obj.data.ID,
                        area: common.isModile() ? ['95%', '95%'] : ['800px', '550px']
                    });
                } else if (obj.event === "delete") {//删除
                    top.layer.confirm('真的要删除么？', function (index) {
                        //在顶层弹出需要手动关闭
                        top.layer.close(index);
                        //请求登入接口
                        view.req({
                            url: 'DeleteMenu' //实际使用请改成服务端真实接口
                            , data: { menuID: obj.data.ID, __RequestVerificationToken: document.getElementsByName("__RequestVerificationToken")[0].value }
                            , type: "post"
                            , dataType: "json"
                            , done: function (res) {
                                if (res.codeIndex == 1) {
                                    layer.msg('删除' + obj.data.ID + res.msg, {
                                        offset: '15px'
                                        , icon: 1
                                        , time: 1000
                                    });
                                    //top.document.getElementById('/Admin/Menu').contentWindow.location.reload(true);

                                    reload();

                                } else {
                                    layer.msg(res.msg, {
                                        offset: 't',
                                        anim: 6
                                    });
                                }

                            }
                        });
                    });
                }
            });
        });

        function reload() {//reload
            treeGrid.reload(tableId);
        }

        function OnAdd() {
            top.layer.open({
                type: 2,
                resize: false,
                id: 'iframe1',
                content: '/Admin/MenuNew',
                area: common.isModile() ? ['95%', '95%'] : ['800px', '550px'],
            });
        }

    </script>
}
